/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.vlivetest20140310;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
import org.joda.time.DateTime;
import org.joda.time.Seconds;

/**
 *
 * @author GiangLT
 */
public class CompareCaptureFileAndRealBroadcast {

    public static void main(String[] args) {
        CompareCaptureFileAndRealBroadcast ccfarb = new CompareCaptureFileAndRealBroadcast();
        ccfarb.doProcess();
    }

    public void doProcess() {
        try {
            DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            String realBroadcastFilePath = "F:\\Projects\\Livescreen\\dump-analysis\\BacNinh20130324\\RealBroadcastTime.csv";
            File realBroadcastFile = new File(realBroadcastFilePath);
            List<String> realBroadcastLines = FileUtils.readLines(realBroadcastFile);
//            List<RealBroadcastTimeslot> realBroadcastTimeslots = Lists.newArrayListWithCapacity(realBroadcastLines.size());
//            for (String line : realBroadcastLines) {
//                String[] tokens = line.split(",");
//                RealBroadcastTimeslot realBroadcastTimeslot = new RealBroadcastTimeslot();
//                DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
//                try {
//                    realBroadcastTimeslot.setStartTime(df.parse(tokens[1]));
//                    realBroadcastTimeslot.setMsgId(tokens[2]);
//                    realBroadcastTimeslot.setPageId(tokens[3]);
//                    realBroadcastTimeslot.setData(tokens[4]);
//                } catch (ParseException ex) {
//                    Logger.getLogger(CompareCaptureFileAndRealBroadcast.class.getName()).log(Level.SEVERE, null, ex);
//                }
//                realBroadcastTimeslots.add(realBroadcastTimeslot);
//            }

            Map<String, Date> dataBroadcastTimeMap = Maps.newHashMapWithExpectedSize(realBroadcastLines.size());
            for (String line : realBroadcastLines) {
                String[] tokens = line.split(",");

                try {
                    Date broadcastTime = df.parse(tokens[0].trim());
                    String data = tokens[3];
                    dataBroadcastTimeMap.put(data.trim(), broadcastTime);
                } catch (ParseException ex) {
                    Logger.getLogger(CompareCaptureFileAndRealBroadcast.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            String broadcastCaptureFilePath = "F:\\Projects\\Livescreen\\dump-analysis\\BacNinh20130324\\BroadcastCapture.csv";
            File broadcastCaptureFile = new File(broadcastCaptureFilePath);
            List<String> broadcastCaptureLines = FileUtils.readLines(broadcastCaptureFile);
            List<BroadcastCapture> broadcastCaptures = Lists.newArrayListWithCapacity(broadcastCaptureLines.size());
            for (String line : broadcastCaptureLines) {
                try {
                    BroadcastCapture broadcastCapture = new BroadcastCapture();
                    String[] tokens = line.split(",");
                    broadcastCapture.setMsgId(tokens[1]);
                    broadcastCapture.setPageId(tokens[2]);
                    String data = tokens[3].trim();
                    broadcastCapture.setData(data);

                    Date receivedTime = df.parse(tokens[0]);
                    broadcastCapture.setReceivedTime(receivedTime);

                    Date realBroadcastTime = dataBroadcastTimeMap.get(data);
                    if (realBroadcastTime != null) {
                        broadcastCapture.setRealBroadcastTime(realBroadcastTime);
                        int secondsBetween = Seconds.secondsBetween(new DateTime(realBroadcastTime), new DateTime(receivedTime)).getSeconds();
                        broadcastCapture.setDelay(secondsBetween);
                    }
                    broadcastCaptures.add(broadcastCapture);
                } catch (Exception ex) {
                    Logger.getLogger(CompareCaptureFileAndRealBroadcast.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

            for (BroadcastCapture broadcastCapture : broadcastCaptures) {
                System.out.println(String.format("%s\t%s\t%s\t%s\t%s\t%s\t%s",
                        df.format(broadcastCapture.getReceivedTime()),
                        broadcastCapture.getRealBroadcastTime() != null ? df.format(broadcastCapture.getRealBroadcastTime()) : "",
                        String.valueOf(broadcastCapture.getDelay()),
                        secondsToTime(broadcastCapture.getDelay()),
                        broadcastCapture.getMsgId(),
                        broadcastCapture.getPageId(),
                        broadcastCapture.getData()));
            }
        } catch (IOException ex) {
            Logger.getLogger(CompareCaptureFileAndRealBroadcast.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private String secondsToTime(Integer seconds) {
        int minutes = 0;
        if (seconds != null) {
            if (seconds < 60) {
                return String.valueOf(seconds) + 's';
            } else {
                minutes = seconds / 60;
                seconds = seconds - minutes * 60;
                return String.format("%sm%ss", minutes, seconds);
            }
        } else {
            return "";
        }
    }

    public class RealBroadcastTimeslot {

        private Date startTime;
        private String msgId;
        private String pageId;
        private String data;

        public RealBroadcastTimeslot() {
        }

        public RealBroadcastTimeslot(Date startTime, String msgId, String pageId, String data) {
            this.startTime = startTime;
            this.msgId = msgId;
            this.pageId = pageId;
            this.data = data;
        }

        public Date getStartTime() {
            return startTime;
        }

        public void setStartTime(Date startTime) {
            this.startTime = startTime;
        }

        public String getMsgId() {
            return msgId;
        }

        public void setMsgId(String msgId) {
            this.msgId = msgId;
        }

        public String getPageId() {
            return pageId;
        }

        public void setPageId(String pageId) {
            this.pageId = pageId;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

    }

    public class BroadcastCapture {

        private Date receivedTime;
        private Date realBroadcastTime;
        private Integer delay;
        private String msgId;
        private String pageId;
        private String data;

        public Date getReceivedTime() {
            return receivedTime;
        }

        public void setReceivedTime(Date receivedTime) {
            this.receivedTime = receivedTime;
        }

        public Date getRealBroadcastTime() {
            return realBroadcastTime;
        }

        public void setRealBroadcastTime(Date realBroadcastTime) {
            this.realBroadcastTime = realBroadcastTime;
        }

        public Integer getDelay() {
            return delay;
        }

        public void setDelay(Integer delay) {
            this.delay = delay;
        }

        public String getMsgId() {
            return msgId;
        }

        public void setMsgId(String msgId) {
            this.msgId = msgId;
        }

        public String getPageId() {
            return pageId;
        }

        public void setPageId(String pageId) {
            this.pageId = pageId;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

    }
}
